package top.wuliaodebaozi2.yunpan;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class SplashActivity extends AppCompatActivity {

    public static final String SP_IS_FIRST_ENTER_APP = "sp_is_first_enter_app";

    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
       setContentView(R.layout.activity_splash);
//       if (isFirstEnterApp()) {
//           startDialog();
//       }else {
//           new Handler().postDelayed(new Runnable() {
//               @Override
//               public void run() {
//                   Intent intent=new Intent();
//                   intent.setClass(SplashActivity.this,MainMenu.class);
//                   startActivity(intent);
//                   finish();//销毁欢迎页面
//               }
//           },3000);//3秒后跳转
//       }
        startDialog();

    }


    private void startDialog() {
        dialog = new AlertDialog.Builder(SplashActivity.this).create();
        dialog.show();
        //对话框弹出后点击或按返回键不消失;
        dialog.setCancelable(false);

        final Window window = dialog.getWindow();
        if (window != null) {
            window.setContentView(R.layout.dialog_intimate);
            window.setGravity(Gravity.CENTER);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //设置属性
            final WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
            params.dimAmount = 0.5f;
            window.setAttributes(params);
            TextView textView = window.findViewById(R.id.tv_1);
            TextView tvCancel= window.findViewById(R.id.tv_cancel);
            TextView tvAgree= window.findViewById(R.id.tv_agree);
            CheckBox cbUserAgreement= window.findViewById(R.id.cb_user_agreement);
            CheckBox cbPrivacyPolicy= window.findViewById(R.id.cb_privacy_policy);
            tvCancel.setOnClickListener(view ->startFinish());
            tvAgree.setOnClickListener(view -> {
                if (cbUserAgreement.isChecked() && cbPrivacyPolicy.isChecked()) {
                    enterApp();
                }else {
                    Toast.makeText(SplashActivity.this,"请先勾选同意用户协议和隐私政策",Toast.LENGTH_SHORT).show();
                }
            });
            String str = "  欢迎使用无聊的包子网盘!" +
                    "为了更好地保障您的个人权益，在您使用我们的产品前，\n" +
                    "请务必审慎阅读《隐私政策》和《用户协议》内的所有条款，\n" +
                    "如果您是14周岁以下的未成年人，您需要和您的监护人一起仔细阅读《儿童隐私保护声明》，\n并征得您的监护人同意后，使用我们的产品、服务或向我们提供信息" +
                    "尤其是:\n1.我们对您的个人信息的收集/保存/使用/对外提供/保护等规则条款，以及您的用户权利等条款; \n" +
                    "2. 约定我们的限制责任、免责条款; \n3.其他以颜色或加粗进行标识的重要条款。如您对以上协议有任何疑问，\n" +
                    "可通过人工客服或发邮件至2440678276@qq.com与我们联系。您点击\"同意并继续”的行为即表示您已阅读完毕并同意以上协议的全部内容。\n" +
                    "如您同意以上协议内容，请点击\"同意并继续”，开始使用我们的产品和服务!\n";
            textView.setText(str);

            SpannableStringBuilder ssb = new SpannableStringBuilder();
            ssb.append(str);
            final int start = str.indexOf("《");//第一个出现的位置
            ssb.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
//                    dialog.cancel();
                    Intent intent = new Intent(SplashActivity.this, PrivacyPolicyAndUserAgreementActivity.class);
                    intent.putExtra("PrivacyPolicyType",0);
                    startActivity(intent);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
//                    ds.setColor(getProxyActivity().getResources().getColor(R.color.text_click_yellow));       //设置文件颜色
                    // 去掉下划线
                    ds.setUnderlineText(false);
                }
            }, start, start + 6, 0);


            final int middle = str.indexOf("《",start+1);//最后一个出现的位置
            ssb.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
//                    dialog.cancel();
                    Intent intent = new Intent(SplashActivity.this, PrivacyPolicyAndUserAgreementActivity.class);
                    intent.putExtra("PrivacyPolicyType",1);
                    startActivity(intent);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
//                    ds.setColor(getProxyActivity().getResources().getColor(R.color.text_click_yellow));       //设置文件颜色
                    // 去掉下划线
                    ds.setUnderlineText(false);
                }
            }, middle, middle + 6, 0);

            final int end = str.lastIndexOf("《");//最后一个出现的位置
            ssb.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
//                    dialog.cancel();
                    Intent intent = new Intent(SplashActivity.this, PrivacyPolicyAndUserAgreementActivity.class);
                    intent.putExtra("PrivacyPolicyType",2);
                    startActivity(intent);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
//                    ds.setColor(getProxyActivity().getResources().getColor(R.color.text_click_yellow));       //设置文件颜色
                    // 去掉下划线
                    ds.setUnderlineText(false);
                }
            }, end, end + 10, 0);
            textView.setMovementMethod(LinkMovementMethod.getInstance());
            textView.setText(ssb, TextView.BufferType.SPANNABLE);
        }
    }

    private void enterApp() {//同意并继续，进入APP
        dialog.cancel();
        saveFirstEnterApp();
        Intent intent=new Intent();
        intent.setClass(SplashActivity.this,MainActivity.class);
        startActivity(intent);
        finish();//销毁欢迎页面
    }

    private void startFinish() {//更改状态，finish APP
        dialog.cancel();
//        _mActivity.finish();
        finish();
    }

    /**
     * 是否是首次进入APP
     */
    public boolean isFirstEnterApp() {
        //获取SharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences("user_login_data",MODE_PRIVATE);
        boolean isFirstEnterApp = sharedPreferences.getBoolean(SP_IS_FIRST_ENTER_APP, true);
        return isFirstEnterApp;
    }

    /**
     * 保存首次进入APP状态
     */
    public void saveFirstEnterApp() {
        //获取SharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences("user_login_data",MODE_PRIVATE);
        //获取Editor对象的引用
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(SP_IS_FIRST_ENTER_APP,false);
        editor.commit();
    }
}